function kingsCalender(options) {

    var dateContainer = $("#date-container");
    var dateText = $("#date-text");
    var dateList = $("#date-list");
    var dateItems = $("#date-items");
    var goUrl = options.defaultValue || ('/showcases?time=');
    var active = options.defaultValue || "active";
    var ContainerShadow = options.defaultValue || "shadow";
    var dateListShadow = options.defaultValue || "shadow";

    var defaultValue = options.defaultValue || formatDate(new Date(time));

    var seconds = 200;
    var initShowTop = 35;
    var dateTimeList = [];
    var calenderLineHeight = options.calenderLineHeight || 30;

    var minTop = 35;

    if (monthList.length > 6) {
        minTop = -32 * (monthList.length - 6) + 35;
    }


    var maxTop = 35;

    //function initDateList() {
    //    var html = "";
    //    var current = new Date();

    //    dateTimeList.push(formatDate(current));
    //    for (var i = 1; i < 36; i++) {
    //        current.setMonth(current.getMonth() - 1);
    //        dateTimeList.push(formatDate(current));
    //    }
    //    dateTimeList.reverse();
    //    for (var i = 0; i < dateTimeList.length; i++) {
    //        html += "<li>" + dateTimeList[i] + "</li>";
    //    }
    //    dateItems.append(html);
    //}

    function initDateList() {
        var html = "";
        for (var i = 0; i < monthList.length; i++) {
            dateTimeList.push(monthList[i].MonthInfo.replace("-", "/"));
        }
        //dateTimeList.reverse();
        for (var i = 0; i < dateTimeList.length; i++) {
            html += "<li>" + dateTimeList[i] + "</li>";
        }
        dateItems.append(html);
    }

    function formatDate(date) {
        var current = date;
        var month = (current.getMonth() + 1);
        var currentMonth = month < 10 ? '0' + month.toString() : month.toString();
        return current.getFullYear() + "/" + currentMonth;
    }

    initDateList();


    function calcPosition(date) {

        var index = dateTimeList.indexOf(date) + 1;
        var tempTop = 35;
        if (index >= 6) {
            var tempTop = calenderLineHeight * (index - 1) * -1 + 2;
        }

        dateItems.find('li').removeClass(active);
        dateItems.find('li').eq(index - 1).addClass(active);


        if (tempTop < minTop) {
            tempTop = minTop;
        } else if (tempTop > maxTop) {
            tempTop = maxTop;
        }
        return tempTop;
    }

    function fixedTop(top) {
        if (top < minTop) {
            top = minTop;
        } else if (top > maxTop) {
            top = maxTop;
        }
        return top;
    }

    function showCalender(top, seconds) {
        dateContainer.addClass(ContainerShadow);
        dateList.addClass(dateListShadow);
        dateList.css({ "display": "block" });
        console.log('top' + top);
        dateItems.stop(true, false).animate({ "top": top + "px" }, seconds);
    }

    function hideCalender() {
        console.log(123);
        dateContainer.removeClass(ContainerShadow);
        dateList.removeClass(dateListShadow);
        dateList.css({ "display": "none" });

    }

    dateContainer.on("click", function (e) {
        var dateValue = dateText.text();
        if (!dateValue) {
            dateValue = defaultValue;
        }
        var position = calcPosition(dateValue);

        showCalender(position, 0);

    });

    function moveStep(step) {

        var top = dateItems.position().top;
        var top = top + calenderLineHeight * step;

        top = fixedTop(top);
        showCalender(top, seconds);
    }


    $("#date-up").on('click', function () {

        moveStep(1);
    });
    $("#date-down").on('click', function () {
        moveStep(-1);
    });



    dateItems.on("click", "li", function (e) {
        e.preventDefault();
        e.stopPropagation();
        var dateStr = $(this).text();
        var year = dateStr.split('/')[0];
        var month = dateStr.split('/')[1];
        dateText.text(dateStr);
        hideCalender();
        window.location.href = goUrl + year + "/" + month + "#weeklist";
    });

    this.moveStep = moveStep;
    this.hide = hideCalender;
    return this;

}

var calender = kingsCalender({});

$(document).ready(function () {

     $('.comments-slider').flexslider({
        animation: "slide",
        direction:"vertical",
        directionNav:false,
        slideshow:false,
        slideshowSpeed:2000
     });


     $('.wb-slider').flexslider({
        animation: "slide",
         direction: "horizontal",  
        directionNav:false,
        slideshow:false,
        slideshowSpeed:1000
     });

     $(" body").click(function () {
         calender.hide();
     });

     $("#date-text,#date-container").click(function (e) {
         $("#date-list").show();
         e.stopPropagation();
     });

     $("#date-text,#date-up,#date-down").click(function (e) {
         e.stopPropagation();
     });

	
});


function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}

function LoadMore() {
    $(".load-more").remove();
    $(".load-icon").show();

    //var hrefTime = $('#date-text').text();
    var hrefTime = GetQueryString("time");
    if (hrefTime == null)
    {
        hrefTime = "";
    }
    var url = "/showcase/list?pageIndex=" + page + "&pageSize=" + size  + '&time=' + hrefTime;
    page++;

    $.get("/api/kings" + url, function (data) {
        $('#showcase-content').css('min-height', '150px');
        setTimeout(function () {
            $('.load-icon').remove();
            $('#showcase-content').append(data);
        }, 200);
    })
}